<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col>
      <span>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean
    }
  },
  methods: {
    operateDepts(option) {
      // 每次点击下拉菜单时, 都会传入一个当前选项的 command 值
      // 根据这个判断后续操作
      if (option === 'add') {
        this.$emit('addDepts', this.data)
        // 添加
      }
      if (option === 'edit') {
        // 编辑
        this.$emit('editDepts', this.data)
      }
      if (option === 'del') {
        // 删除
        this.$confirm('是否确认删除该部门?', '删除部门', {
          confirmButtonText: '赶快',
          cancelButtonText: '我错了',
          type: 'warning'
        }).then(() => {
          // 这里是处理点击确认的情况
          // 真正的删除要等到添加完成后再来实现
          delDepartments(this.data.id).then(() => {
            console.log('这里是删除成功')
            // 也得通知父组件, 更新数据
            this.$emit('delDepts')
          }).catch(() => {
            console.log('这里是删除失败')
          })
        }).catch(() => {
          // 这里是取消的逻辑
        })
      }
    }
  }
}
</script>

<style>

</style>
